package com.guogee.pdb.dao;

import com.guogee.pdb.bean.UnlockedRecord;
import com.guogee.pdb.db.DBHelper;
import com.guogee.pdb.db.DBHelper.AlarmRecordCollection;
import com.guogee.pdb.db.DBHelper.UnlockedRecordCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class UnlockedRecordDAO extends ADao<UnlockedRecord> {
	public UnlockedRecordDAO(){
		dbCollection = dbHelper.getIsmartDB().getCollection(DBHelper.UnlockedRecordCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(UnlockedRecord obj) {
		// TODO Auto-generated method stub
		if(insertItem1(obj) != null){
			return true;
		}
		return false;

	}

	@Override
	public UnlockedRecord parstItemFromCursor(DBCursor cursor) {
		// TODO Auto-generated method stub
		return parstDBObject(cursor.next());
	}

	@Override
	public String insertItem1(UnlockedRecord obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(UnlockedRecordCollection.DEVICE_ADDR, obj.getDeviceAddr());
		dbObj.put(UnlockedRecordCollection.OPEN_TIME, obj.getOpenTime());
		dbObj.put(UnlockedRecordCollection.USER_ID, obj.getUserId());
		dbObj.put(UnlockedRecordCollection.OPEN_METHOD, obj.getOpenMethod());
		dbObj.put(UnlockedRecordCollection.INVISIBLE, obj.getInvisible());
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(UnlockedRecordCollection.ID).toString();
		}
		return null;
	}

	@Override
	public UnlockedRecord parstDBObject(DBObject obj) {
		// TODO Auto-generated method stub
		UnlockedRecord recObj = new UnlockedRecord();
		recObj.setId(obj.get(UnlockedRecordCollection.ID).toString());
		recObj.setDeviceAddr(obj.get(UnlockedRecordCollection.DEVICE_ADDR).toString());
		recObj.setOpenTime(Long.valueOf(obj.get(UnlockedRecordCollection.OPEN_TIME).toString()));
		recObj.setUserId(obj.get(UnlockedRecordCollection.USER_ID).toString());
		recObj.setOpenMethod(Byte.valueOf(obj.get(UnlockedRecordCollection.OPEN_METHOD).toString()));
		if (obj.get(AlarmRecordCollection.INVISIBLE) != null) {
			recObj.setInvisible(Byte.parseByte(obj.get(AlarmRecordCollection.INVISIBLE).toString()));
		} 
		return recObj;
	}
	
	

}
